//
//  SLTeenagerConfirmPWDVC.m
//  BXlive
//
//  Created by sweetloser on 2020/8/31.
//  Copyright © 2020 cat. All rights reserved.
//

#import "SLTeenagerConfirmPWDVC.h"
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <Masonry/Masonry.h>
#import "GradientButton.h"
@interface SLTeenagerConfirmPWDVC ()

@property(nonatomic,strong)UIView *navigationView;

@property(nonatomic,strong)GradientButton *nextBtn;

@property(nonatomic,strong)UITextField *pwdTF;

@property(nonatomic, assign) NSInteger itemCount;

@property(nonatomic,strong)NSMutableArray *labels;

@property(nonatomic,strong)NSMutableArray *lines;

@end

@implementation SLTeenagerConfirmPWDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    _itemCount = 4;
    _labels = [NSMutableArray new];
    _lines = [NSMutableArray new];
    [self setupNavigationView];
    [self setupOthersView];
    // Do any additional setup after loading the view.
}

-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"确认密码", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
}

-(void)setupOthersView{
    WS(weakSelf);
    UILabel *pwdTL = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"确认密码", nil) Font:SLBFont(__ScaleWidth(24)) TextColor:sl_textColors];
    [self.view addSubview:pwdTL];
    
    [pwdTL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(21));
        make.height.mas_equalTo(__ScaleWidth(33));
        make.right.mas_equalTo(-__ScaleWidth(21));
        make.top.equalTo(weakSelf.navigationView.mas_bottom).offset(__ScaleWidth(15));
    }];
    UIView *pwdView = [UIView new];
    [self.view addSubview:pwdView];
    [pwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(__ScaleWidth(93));
        make.top.equalTo(pwdTL.mas_bottom);
    }];
    
    UITextField *textF = [[UITextField alloc] init];
    [pwdView addSubview:textF];
    [textF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(__ScaleWidth(40));
        make.top.mas_equalTo(__ScaleWidth(13));
    }];
    textF.textColor = SLClearColor;
    textF.tintColor= [UIColor clearColor];
    textF.keyboardType  = UIKeyboardTypeNumberPad;
    _pwdTF = textF;
//    textF.delegate = self;
    [textF addTarget:self action:@selector(tfEditingChanged:) forControlEvents:(UIControlEventEditingChanged)];
    for (NSInteger i = 0; i < self.itemCount; i++) {
        UILabel *label = [UILabel new];
        label.textAlignment = 1;
        label.font = SLBFont(__ScaleWidth(18));
        label.textColor = sl_textColors;
        [pwdView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(__ScaleWidth(82 * i) + __ScaleWidth(36));
            make.top.equalTo(textF);
            make.width.mas_equalTo(__ScaleWidth(58));
            make.bottom.equalTo(textF);
        }];
        [self.labels addObject:label];
        
        UIView *line = [UIView new];
        [pwdView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(label.mas_bottom);
            make.width.equalTo(label);
            make.height.mas_equalTo(3);
            make.left.equalTo(label);
        }];
        line.backgroundColor = sl_divideLineColor;
        line.layer.cornerRadius = 1.5;
        line.layer.masksToBounds = YES;
        [self.lines addObject:line];
        
    }
    
//    下一步按钮
    UIButton *nextBtn = [[GradientButton alloc] initWithFrame:CGRectZero];
    [self.view addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(__ScaleWidth(-12));
        make.height.mas_equalTo(__ScaleWidth(44));
        make.top.equalTo(pwdView.mas_bottom);
    }];
    [nextBtn setTitle:NSLocalizedString(@"确定", nil) forState:BtnNormal];
//    [nextBtn setTitleColor:sl_whiteTextColors forState:BtnSelected];
    [nextBtn setTitleColor:sl_whiteTextColors forState:BtnNormal];
    [nextBtn setAdjustsImageWhenHighlighted:NO];
//    [nextBtn setBackgroundImage:[UIImage imageWithColor:sl_normalColors] forState:BtnSelected];
    [nextBtn setBackgroundImage:[UIImage imageWithColor:sl_subBGColors] forState:BtnNormal];
    
    [nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.layer.masksToBounds = YES;
    nextBtn.layer.cornerRadius = __ScaleWidth(22);
    _nextBtn = nextBtn;
}

-(void)nextBtnAction{
    NSLog(NSLocalizedString(@"确定", nil));
    if (_pwdTF.text.length < _itemCount) {
        [SLProgressHUD slShowInfoWithMessage:[NSString stringWithFormat:NSLocalizedString(@"请输入%ld位密码", nil),(long)_itemCount]];
        return;
    }
    
    NSString *nowPwd = [_pwdTF.text substringWithRange:NSMakeRange(0, _itemCount)];
    if (![nowPwd isEqualToString:self.myPwd]) {
        [SLProgressHUD slShowInfoWithMessage:[NSString stringWithFormat:NSLocalizedString(@"密码不正确，请重新输入", nil)]];
        return;
    }
    
//    确认过密码
    WS(weakSelf);
    [[NewHttpManager sharedNetManager] POST:@"s=User.setTeenagerPass" parameters:@{@"password":nowPwd} success:^(id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
        NSNumber *code = responseObject[@"code"];
        NSString *msg = responseObject[@"msg"];
        [SLProgressHUD slShowInfoWithMessage:msg];
        if ([code integerValue] == 0) {
//            开启成功
            
            BXLiveUser *lv = [BXLiveUser currentBXLiveUser];
            lv.teenager_model_open = @"1";
            [BXLiveUser setCurrentBXLiveUser:lv];
            
//            [weakSelf popToRootVc];
            UINavigationController *nvc = weakSelf.navigationController;
            [nvc popToRootViewControllerAnimated:NO];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kDidOpenTeenNotification object:nil userInfo:nil];
        }
        
        
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
    
}

- (void)tfEditingChanged:(UITextField *)textField
{
  if (textField.text.length > self.itemCount) {
    textField.text = [textField.text substringWithRange:NSMakeRange(0, self.itemCount)];
  }
  for (int i = 0; i < self.itemCount; i++) {
      UILabel *label = [self.labels objectAtIndex:i];
      UILabel *line = [self.lines objectAtIndex:i];
      if (i < textField.text.length) {
          label.text = [textField.text substringWithRange:NSMakeRange(i, 1)];
          line.backgroundColor = SLLineColor;
      } else {
          label.text = @"";
          line.backgroundColor = sl_divideLineColor;
    }
  }
  // 输入完毕后，自动隐藏键盘
  if (textField.text.length >= self.itemCount) {
      [textField resignFirstResponder];
//      _nextBtn.selected = YES;
//      _nextBtn.userInteractionEnabled = YES;
      [_nextBtn setBackgroundImage:[UIImage imageWithColor:sl_normalColors] forState:BtnNormal];
      [_nextBtn setTitleColor:sl_whiteTextColors forState:BtnNormal];
  }else{
//      _nextBtn.selected = NO;
//      _nextBtn.userInteractionEnabled = NO;
      [_nextBtn setBackgroundImage:[UIImage imageWithColor:sl_subBGColors] forState:BtnNormal];
      [_nextBtn setTitleColor:sl_textColors forState:BtnNormal];
  }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
